import sys, urllib2, re
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *


class Render(QWebPage):
    def __init__(self, url, XSSResult):
        self.app = QApplication(sys.argv)
        QWebPage.__init__(self)
        self.loadFinished.connect(self._loadFinished)
        self.mainFrame().load(QUrl(url))
        self.result = False
        self.XSSResult = XSSResult
        self.app.exec_()

    def _loadFinished(self, result):
        self.frame = self.mainFrame()
        self.app.quit()

    def getHtml(self):
        return self.frame.toHtml().toUtf8()

    def javaScriptAlert(self, frame, message):
        if not self.result and message == self.XSSResult:
            self.result = True


class CheckXSS():
    def __init__(self, url, XSSCode, XSSResult):
        r = Render(url+XSSCode, XSSResult)

        self.result = r.result

    def getResult(self):
        return self.result


def main():
    # xss = "alert('asdf');"
    # result = 'asdf'

    # cxss = CheckXSS('http://localhost/test/index.php?p=', xss, 'asdf')

    # print cxss.getResult()
    absulateUrlPattern = re.compile(
        # r"(https?://)"
        # r"(\S+)(/\S*$)?"
        # r"(((:?http|https)://)?"
        # r"(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})))"
        r"(\.\S[^/]+)$", re.IGNORECASE)

    print absulateUrlPattern.search('https://ashdj.com/12123/asdf/asdf.html').groups()
main()